package com.water.controller;

import com.water.entity.SyArea;
import com.water.entity.SyEmp;
import com.water.entity.SyMenu;
import com.water.service.SyAreaService;
import com.water.service.SyEmpService;
import com.water.service.SyMenuService;
import com.water.utils.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * (SyEmp)表控制层
 *
 * @author makejava
 * @since 2020-12-14 19:19:00
 */
@Controller
@RequestMapping("syEmp")
public class SyEmpController {

    // XXX为要打印日志的类名
    public static final Logger logger = LoggerFactory.getLogger(SyEmpController.class);

//  在需要打印的方法中，用此方式输出日志

    /**
     * 服务对象
     */
    @Resource
    private SyEmpService syEmpService;

    @Resource
    private SyMenuService syMenuService;

    @Resource
    private SyAreaService syAreaService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SyEmp selectOne(Integer id) {
        return this.syEmpService.queryById(id);
    }




    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(Model model){
        model.addAttribute("cuo","");
        System.out.println("跳转到登录页面");
        return "login";
    }

    /**
     * 登录
     * @param syemp
     * @param model
     * @param
     * @return
     */
    @RequestMapping(value = "loginS",method = RequestMethod.POST)
    public String select(SyEmp syemp, Model model, HttpServletRequest request,HttpSession session,SyArea syArea){
        Subject currentSubject = SecurityUtils.getSubject();
        logger.info("要输出的日志信息");
        String md5=   MD5Utils.md5(syemp.getPwd());  // md5加密
        syemp.setPwd(md5);
        syemp.setDisabled(0);
        this.syEmpService.querySy(syemp);
        if(this.syEmpService.querySy(syemp)==null){
            System.out.println("用户名或密码不对");
        }
        else if(!isEmpty(this.syEmpService.querySy(syemp))){
            UsernamePasswordToken token =
                    new UsernamePasswordToken(syemp.getEmpno(), syemp.getPwd());
            token.setRememberMe(true);//记录用户
            try {
                currentSubject.login(token);//调用Subject的login方法执行登录
            } catch (AuthenticationException e) {//所有认证时异常的父类
                System.out.println("登录失败："+e.getMessage());
            }

            System.out.println("认证成功");
            SyEmp syemp1=    this.syEmpService.querySy(syemp);
            session.setAttribute("user",syemp1);
            model.addAttribute("name",syemp1.getEmpname());
            return "workspace";
        }
        model.addAttribute("cuo","用户名或密码错误");  // 不正确返回一句提示
        return "login";
    }


    /**
     * 跳转到修改密码
     * @return
     */
    @RequestMapping(value = "sys_pwd",method = RequestMethod.GET)
    public String sys_pwd(HttpSession httpSession, Model model){
        System.out.println("进入修改密码");
        SyEmp syemp= (SyEmp) httpSession.getAttribute("user");
        System.out.println(syemp.toString()+"修改密码");
        model.addAttribute("name",syemp.getEmpname());
        model.addAttribute("pwd",syemp.getPwd());
        model.addAttribute("msg","");
        return "sys/sys_pwd";
    }

    /**
     * 修改密码操作
     * @param name
     * @param name1
     * @param syemp
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "sys_pwdx",method = RequestMethod.POST)
    public String sys_pwdx(@RequestParam("lname1") String name,
                           @RequestParam("lname2") String name1,
                           SyEmp syemp,
                           HttpSession httpSession,
                           Model model){
        if(name.equals(name1)){
            SyEmp syemp1= (SyEmp) httpSession.getAttribute("user");
            syemp.setPwd(MD5Utils.md5(name));
            syemp.setId(syemp1.getId());
            syemp=  this.syEmpService.update(syemp);
            System.out.println(syemp+"修改后的数据");
            return "success";
        }
        if(!name.equals(name1)){
            model.addAttribute("msg","修改失败");
            return "sys/sys";
        }

        return "sys/sys_pwd";
    }

    /**
     * 退出登录
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "loginout")
    public String loinout(HttpSession httpSession){
        SyEmp syemp= (SyEmp) httpSession.getAttribute("user");
        if(!isEmpty(syemp)){
            httpSession.removeAttribute("user");
        }
        return "redirect:/syEmp/login";

    }

    /**
     * 跳转到员工管理
     * @return
     */
    @RequestMapping(value = "sys_emp",method = RequestMethod.GET)
    public String sys_emp(Model model,SyEmp syemp){
        syemp.setDisabled(0);
        List<SyEmp> syempList= this.syEmpService.queryAll(syemp);
        System.out.println(syempList);
        model.addAttribute("list",syempList);
        return "sys/sys_emp";
    }

    /**
     * 跳转到添加员工
     * @return
     */
    @RequestMapping(value = "sys_emp_add",method = RequestMethod.GET)
    public String sys_emp_add(Model model){
        model.addAttribute("msg","");
        return "sys/sys_emp_add";
    }

    /**
     * 添加员工操作
     * @return
     */
    @RequestMapping(value = "sys_empadd",method = RequestMethod.POST)
    public String sys_empadd(SyEmp syemp,Model model){
        System.out.println("进入添加员工");
        System.out.println(syemp.toString());
        syemp.setPwd(     MD5Utils.md5(syemp.getPwd()));
        try{
            SyEmp syemp1=   this.syEmpService.insert(syemp);
            System.out.println(syemp1.toString());
            if(!isEmpty(syemp1)){
                System.out.println("添加成功");
                return "success";
            }
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("添加失败");
            model.addAttribute("msg","工号已有");
            return "sys/sys_emp_add";
        }


        System.out.println("添加失败");
        return "sys/sys_emp_add";
    }

    /**
     * 跳转到id的值绑到修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "sys_emp_update/{id}",method = RequestMethod.GET)
    public String sys_emp_update(Model model,@PathVariable("id") Integer id){
        System.out.println(id+"id的值");
        SyEmp syemp= this.syEmpService.queryById(id);
        model.addAttribute("syemp",syemp);
        return "sys/sys_emp_update";
    }

    /**
     * 修改员工数据
     * @param syemp
     * @param model
     * @return
     */
    @RequestMapping(value = "sys_emp_update1",method = RequestMethod.POST)
    public String sys_emp_update(SyEmp syemp,Model model){
        System.out.println(syemp.toString());
        try{
            SyEmp   syemp1=  this.syEmpService.update(syemp);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
        }
        model.addAttribute("msg","工号已有");
        return "sys/sys_emp_update";
    }

    /**
     * 删除员工
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "sys_empdel/{id}",method= RequestMethod.GET)
    public String sys_empdel(@PathVariable("id") Integer id,Model model){
        System.out.println(id);
    boolean bo=    this.syEmpService.deleteById(id);
    if (bo==true){
        return "redirect:/syEmp/sys_emp";
    }
        return "redirect:/syEmp/sys_emp";
    }
}